package com.cell.controller;

import com.cell.common.Result;
import com.cell.common.States;
import com.cell.dao.user.UserDao;
import com.cell.pojo.DTO.order.CommitOrderDTO;
import com.cell.pojo.DTO.order.overOrderDTO;
import com.cell.pojo.DTO.order.acceptOrderDTO;
import com.cell.pojo.DTO.order.commitErrorOrderDTO;
import com.cell.service.order.IOrderService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cloudJiang
 * @since 2022-06-04
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单操作相关接口")
public class OrderController {
    @Autowired
    IOrderService orderService;
    @Autowired
    UserDao userDao;

    /**
     * 卖方提前结束订单
     *
     * @return
     */
    @ApiOperation(value = "卖方提前结束订单", notes = "只有当订单是已完成但未结束状态才能提前结束。")
    @GetMapping("/endOrder")
    Result endOrder(@ApiParam(name = "id", value = "订单id", required = true, type = "Long") @RequestParam("id") Long id) {
        orderService.endOrder(id);
        return new Result(null,
                States.OK.getStates(),
                States.OK.getDescription());
    }

    /**
     * 买方完成货物收取，订单完成，更改订单状态为2,计算出总价格
     */
    @ApiOperation(value = "完成订单", notes = "买方完成货物收取，订单完成，更改订单状态为2,计算出总价格")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单id", paramType = "Long", required = true),
            @ApiImplicitParam(name = "weight", value = "物品实际重量，格式：1.00", paramType = "BigDecimal", required = true),
            @ApiImplicitParam(name = "overOrderDTO", value = "以上信息封装到该dto")
    })
    @PostMapping("/overOrder")
    Result overOrder(@Validated @RequestBody overOrderDTO overOrderDTO) {
        orderService.overOrder(overOrderDTO.getId(), overOrderDTO.getWeight());
        return new Result(null,
                States.OK.getStates(),
                States.OK.getDescription());
    }

    /**
     * 提交订单异常
     *
     * @return
     */
    @ApiOperation(value = "订单异常提交", notes = "订单无法正常完成，提交异常")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单id", paramType = "Long", required = true),
            @ApiImplicitParam(name = "errorMessage", value = "异常信息", required = true),
            @ApiImplicitParam(name = "commitErrorOrderDTO", value = "以上信息封装到该dto")
    })
    @PostMapping("/commitErrorOrder")
    Result commitErrorOrder(@Validated @RequestBody commitErrorOrderDTO commitErrorOrderDTO) {
        orderService.commitErrorOrder(commitErrorOrderDTO.getId(),
                commitErrorOrderDTO.getErrorMessage());
        return new Result(null, States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 提交订单
     *
     * @return
     */
    @ApiOperation(value = "发布订单", notes = "用户订单发布")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "salId", value = "卖方id", paramType = "Long", required = true),
            @ApiImplicitParam(name = "name", value = "物品名称", required = true),
            @ApiImplicitParam(name = "type", value = "物品类型", required = true),
            @ApiImplicitParam(name = "weight", value = "物品大概重量，格式：1.00", paramType = "BigDecimal", required = true),
            @ApiImplicitParam(name = "commitOrderDTO", value = "以上信息封装到该dto")
    })
    @PostMapping("/commitOrder")
    Result commitOrder(@Validated @RequestBody CommitOrderDTO commitOrderDTO) {
        orderService.commitOrder(
                commitOrderDTO.getSalId(), commitOrderDTO.getName(),
                commitOrderDTO.getType(), commitOrderDTO.getWeight());
        return new Result(null, States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 查询和自己有关的订单
     *
     * @return
     */
    @ApiOperation(value = "获取自己的订单", notes = "获取自己的订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "Long", required = true),
            @ApiImplicitParam(name = "orderState", value = "订单状态，详情见entity的order类", required = true)
    })
    @GetMapping("/getMyOrder")
    Result getAllOrder(@RequestParam("userId") Long userId, @RequestParam("orderState") Integer orderState) {
        return new Result(orderService.getAllOrder(orderState, userId),
                States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 代理用户接受订单
     *
     * @return
     */
    @ApiOperation(value = "接受订单", notes = "代理用户接受订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单id", paramType = "Long", required = true),
            @ApiImplicitParam(name = "buyId", value = "买方id，即当前接受订单用户id", required = true),
            @ApiImplicitParam(name = "acceptOrderDTO", value = "以上信息封装到该dto")
    })
    @PostMapping("/acceptOrder")
    Result acceptOrder(@Validated @RequestBody acceptOrderDTO acceptOrderDTO) {
        orderService.acceptOrder(acceptOrderDTO.getId(),
                acceptOrderDTO.getBuyId());
        return new Result(null, States.OK.getStates(), States.OK.getDescription());
    }


    /**
     * 用户删除已完成订单
     *
     * @return
     */
    @ApiOperation(value = "删除订单", notes = "用户删除订单已完成的订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单id", paramType = "Long", required = true)
    })
    @GetMapping("/deleteOrder")
    Result deleteOrder(@RequestParam Long id) {
        orderService.deleteOrder(id);
        return new Result(null,
                States.OK.getStates(),
                States.OK.getDescription());
    }

    /**
     * 代理查看附近可接的订单
     *
     * @param state
     * @param locationId
     * @return
     */
    @ApiOperation(value = "查看附近的订单", notes = "代理通过代理级别来获取附近订单，级别越高，获取区域越大")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "locationId", value = "代理位置id", paramType = "Long", required = true),
            @ApiImplicitParam(name = "state", value = "代理级别", paramType = "Long", required = true)
    })
    @GetMapping("/getNearbyOrder")
    Result getNearbyOrder(Integer state, Long locationId) {
        return new Result(orderService.getNearbyOrder(state, locationId),
                States.OK.getStates(),
                States.OK.getDescription());
    }
}

